<template>
    <div id="app" v-cloak>
        <div class="content-header">

<h1>用户管理</h1>
<br />
<!--用户管理按钮-->
<el-button @click="Manage" type="info"  style="width: 30%;font-size: 20px;" >
        去用户管理
    </el-button>
<!--公告按钮-->
<el-button @click="Bulletin" type="success" style="width:30%;font-size: 20px;" plain>
      去发布公告
    </el-button>
    <!--用户反馈按钮-->
    <el-button @click="Help" type="success" style="width: 30%;font-size: 20px;" plain>
        去用户反馈
    </el-button>
    
</div>

<div class="app-container">

<div class="box">

    <div class="filter-container">
        <el-input placeholder="账号" v-model="uid" style="width: 200px;" class="filter-item"></el-input>
        <el-button @click="cx" >查询</el-button>
        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
    </div>

    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>

        <el-table-column type="index" align="center" label="序号"></el-table-column>

        <el-table-column prop="uid" label="账号" align="center"></el-table-column>

        <el-table-column prop="password" label="密码" align="center"></el-table-column>

        <el-table-column prop="judge" label="权限" align="center"></el-table-column>

        <el-table-column prop="kkey" label="key" align="center"></el-table-column>

        <el-table-column label="操作" align="center">

            <template slot-scope="scope">

                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>

                <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>

            </template>

        </el-table-column>

    </el-table>

    <!--分页组件-->
    <div class="pagination-container">

        <el-pagination
                class="pagiantion"

                @current-change="handleCurrentChange"

                :current-page="pagination.currentPage"

                :page-size="pagination.pageSize"

                layout="total, prev, pager, next, jumper"

                :total="pagination.total">

        </el-pagination>

    </div>
    

    <!-- 新增标签弹层 -->

    <div class="add-form">

        <el-dialog title="新增账号" :visible.sync="dialogFormVisible">

            <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">

                <el-row>

                    <el-col :span="11">

                        <el-form-item label="账号" prop="uid">

                            <el-input v-model="formData.uid"/>

                        </el-form-item>

                    </el-col>

                    <el-col :span="11">

                        <el-form-item label="密码" prop="password">

                            <el-input v-model="formData.password"/>

                        </el-form-item>

                    </el-col>

                </el-row>


                <el-row>

                    <el-col :span="11">

                        <el-form-item label="权限" prop="judge">

                            <el-input v-model="formData.judge" ></el-input>

                        </el-form-item>
                        

                    </el-col>

                    <el-col :span="11">

                        <el-form-item label="key" prop="key">

                            <el-input v-model="formData.key" ></el-input>

                        </el-form-item>

                    </el-col>

                </el-row>

            </el-form>

            <div slot="footer" class="dialog-footer">

                <el-button @click="cancel()">取消</el-button>

                <el-button type="primary" @click="handleAdd()">确定</el-button>

            </div>

        </el-dialog>

    </div>

    <!-- 编辑标签弹层 -->

    <div class="add-form">

        <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">

            <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">

                <el-row>

                    <el-col :span="12">

                        <el-form-item label="账号" prop="uid">

                            <el-input v-model="formData.uid"/>

                        </el-form-item>

                    </el-col>

                    <el-col :span="12">

                        <el-form-item label="密码" prop="password">

                            <el-input v-model="formData.password"/>

                        </el-form-item>

                    </el-col>

                </el-row>

                <el-row>

                    <el-col :span="12">

                        <el-form-item label="权限">

                            <el-input v-model="formData.judge"></el-input>

                        </el-form-item>

                    </el-col>

                    <el-col :span="12">

                        <el-form-item label="key">

                            <el-input v-model="formData.kkey"></el-input>

                        </el-form-item>

                    </el-col>

                </el-row>

            </el-form>

            <div slot="footer" class="dialog-footer">

                <el-button @click="cancel()">取消</el-button>

                <el-button type="primary" @click="handleEdit()">确定</el-button>

            </div>

        </el-dialog>

    </div>

</div>
    
</div>
    </div>
  </template>

<script>
  export default {
    components: {
      
    },
    data(){
        return {
            dataList: [],//当前页要展示的列表数据
            dialogFormVisible: false,//添加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            formData: {},//表单数据
            param:[],
            uid:"",
            rules: {//校验规则
                uid: [{ required: true, message: '账号为必填项', trigger: 'blur' }],
                password: [{ required: true, message: '密码为必填项', trigger: 'blur' }],
                judge: [{ required: true, message: '权限为必填项', trigger: 'blur' }],
                key: [{ required: true, message: 'key为必填项', trigger: 'blur' }],
            },
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:7,//每页显示的记录数
                total:0,//总记录数
            }
        }
    },
    created() {
            //调用查询全部数据的操作
            this.getAll();
        },

        methods: {
            getAll() {
                        this.$axios({
                        url:"http://localhost/users/"+this.pagination.currentPage+"/"+this.pagination.pageSize,
                        method:"get",
                        data:{
                        },
                    })
                        .then((res)=>{
                            this.dataList = res.data.data.records;
                            this.pagination.pageSize = res.data.data.size;
                            this.pagination.currentPage =res.data.data.current;
                            this.pagination.total= res.data.data.total;
                        
                    })
                        .catch((err)=>{
                        console.log(err);
                        });
            },

            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.getAll();
            },

            //弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
            },

            //重置表单
            resetForm() {
                this.formData = [];
            },

            //添加
            handleAdd () {
                        this.$axios({
                        url:"http://localhost/users",
                        method:"post",
                        data:{
                            uid:this.formData.uid,
                            password:this.formData.password,
                            judge:this.formData.judge,
                            kkey:this.formData.key,
                        },
                    }).then((res)=>{
                        if(res.data.flag){
                            this.dialogFormVisible = false;
                        this.$message.success("添加成功");
                        this.getAll();
                        }
                        else {this.$message.error(res.data.msg);
                        this.getAll();}
                        
                    })
            },

            //取消
            cancel(){
                this.resetForm();
                this.dialogFormVisible = false;
                this.dialogFormVisible4Edit = false;
            },

            // 删除
            handleDelete(row) {

                    this.$confirm("此操作永久删除当前信息，是否继续？","提示",{type:"info"}).then(()=>{
                                this.$axios({
                                url:"http://localhost/users/"+row.id,
                                method:"delete",
                                data:{

                                },
                            }).then((res)=>{
                                if(res.data.flag){
                                this.$message.success("删除成功");
                                this.getAll();
                                }
                                else{
                                    this.$message.error("数据同步失败，自动刷新！");
                                    this.getAll();
                                }
                            })
                            }).catch(()=>{
                        });
            },

            //弹出编辑窗口
            handleUpdate(row) {
                this.$axios({
                        url:"http://localhost/users/"+row.id,
                        method:"get",
                        data:{
                        },
                    })
                        .then((res)=>{
                            if(res.data.flag && res.data.data != null){
                                this.dialogFormVisible4Edit = true;
                                this.formData = res.data.data;
                                this.formData.password = "";
                                this.formData.kkey = "";
                            }else{
                                this.$message.error("数据同步失败，自动刷新！");
                                this.getAll();
                            }
                            
                        
                    })
            },

            //跳转公告
            Bulletin(){
                this.$router.push({ path: '/Bulletin/'+"123"})
            },

            //跳转反馈
            Help(){
                this.$router.push({ path: '/Help/'+"123"})
            },

            //修改
            handleEdit() {
                if(this.formData.password != "" && this.formData.kkey !=""){
                this.$axios({
                        url:"http://localhost/users",
                        method:"put",
                        data:{
                            id:this.formData.id,
                            uid:this.formData.uid,
                            password:this.formData.password,
                            judge:this.formData.judge,
                            kkey:this.formData.kkey,
                        },
                    }).then((res)=>{
                        if(res.data.flag){
                            this.dialogFormVisible4Edit = false;
                        this.$message.success("修改成功");
                        this.getAll();
                        }
                        else {this.$message.error("修改失败");
                        this.getAll();}
                        
                    })
                }else this.$message.error("请完整填写")
            },
            cx(){
                if(this.uid == ""){
                    this.getAll();
                } 
                else{
                    this.$axios({
                        url:"http://localhost/users/cx/"+this.uid,
                        method:"get",
                        data:{
                        },
                    }).then((res)=>{
                            this.dataList = res.data.data;
                    })
                }
                
            }
        }
  }
  </script>
<style src="@/css/style.css"></style> 
<style scoped>
[v-cloak]{
  display:none;
}
#app{
    height:100vh;
}
</style>